package com.winz.cultureapp.activity;

import android.content.Intent;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mine.xrecyclerview.XRecyclerView;
import com.mine.xrecyclerview.rcvadapter.OnItemClickListener;
import com.winz.cultureapp.R;
import com.winz.cultureapp.adapter.xrcv.MessageDataAdapter;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.MessageListBean;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.request.RequestFactory;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;
import com.winz.cultureapp.utils.SharePrefsUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的消息
 */
public class MessageActivity extends BaseAppCompatActivity implements OnItemClickListener, XRecyclerView.LoadingListener {
    @BindView(R.id.tv_top_title)
    TextView mTv_top_title;
    @BindView(R.id.xrcv_data)
    public XRecyclerView mXrcv_data;

    private boolean isRefresh = true;
    private int limit = 10;
    private int page = 1;
    private int totalPage = 0;
    
    private List<MessageListBean.DataBean.ListBean> mDatas;
    private MessageDataAdapter mAdapter;
    
    @Override
    protected int contentView() {
        return R.layout.activity_message;
    }
    @Override
    protected void initViews() {
        mTv_top_title.setText(getString(R.string.mine_top_item4));
        mDatas = new ArrayList<>();
        // XrecyclerView
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mXrcv_data.setLayoutManager(manager);
        mXrcv_data.setPullRefreshEnabled(true);
        mXrcv_data.setLoadingMoreEnabled(true);
        mAdapter = new MessageDataAdapter(this,mDatas,R.layout.empty_view,R.layout.item_message_list);
    }

    @Override
    protected void initEvents() {

        mXrcv_data.setAdapter(mAdapter);
        mXrcv_data.setLoadingListener(this);
        mAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void loadDatas(Intent intent) {
        getList();
    }

    

    /**
     * 获取数据列表
     */
    private void getList() {
        showProgressDialog();
        HttpUitls.onGetByToken("user_message",
                SharePrefsUtil.getInstance().getString(Constants.USER_INFO.TOKEN),
                RequestFactory.getInstance().user_message(page+"",limit+""),
                new OnResponeListener<MessageListBean>() {
            @Override
            public void onSuccess(MessageListBean Bean) {
                Logger.d("user_message==>>", JsonUtil.getJsonFromObj(Bean));
                if (Bean.code == 0){
                    totalPage = Bean.totalPage;
                    if (Bean.data.list != null && Bean.data.list.size()>0){
                        mXrcv_data.setLoadingMoreEnabled(true);
                        if (isRefresh){
                            mDatas.clear();
                            mDatas = Bean.data.list;
                        }else {
                            mDatas.addAll(Bean.data.list);
                        }
                    }else {
                        mDatas.clear();
                        MessageListBean.DataBean.ListBean empty = new MessageListBean.DataBean.ListBean();
                        empty.layout_type = -1;
                        mDatas.add(empty);
                        mXrcv_data.setLoadingMoreEnabled(false);
                    }
                    mAdapter.setDatas(mDatas);
                }else {
                    showMsg(Bean.msg);
                }
                if (isRefresh){
                    mXrcv_data.refreshComplete();
                }else {
                    mXrcv_data.loadMoreComplete();
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                dismissProgressDialog();
                Logger.d("user_message==>>", error);
                if (isRefresh){
                    mXrcv_data.refreshComplete();
                }else {
                    mXrcv_data.loadMoreComplete();
                }
            }
        });

    }

    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        isRefresh = true;
        page = 1;
        getList();
    }

    /**
     * 加载
     */
    @Override
    public void onLoadMore() {
        isRefresh = false;
        if (page >= totalPage) {
            mXrcv_data.setNoMore(true, true, "没有更多数据了");
        } else {
            page++;
            getList();
        }
    }

    @Override
    public void onItemClick(ViewGroup parent, View view, int position) {
//        Bundle bundle = new Bundle();
//        bundle.putString(Constants.KEY.NEWS.ID,mDatas.get(position).id);
//        bundle.putString(Constants.KEY.NEWS.TOP_TITLE,title);
//        bundle.putString(Constants.KEY.NEWS.HREF,"https://mini.eastday.com/a/n190117072753205.html?qid=02012");
//        startActivity(NewsDesActivity.class,false,bundle);
    }

    @Override
    public boolean onItemLongClick(ViewGroup parent, View view, int position) {
        return false;
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {
        
    }

    @OnClick({R.id.fl_back})
    public void click(View view){
        switch (view.getId()){
            case R.id.fl_back:
                finish();
                break;
        }
    }
}
